<?php

class xml_yp{
	var $_parser;
	
	var $_filename;
	var $_data;
	var $_index;
	var $_struct;
	
	function xml_yp($filename){
		$this->_parser = xml_parser_create() or die("Error creating xml parser.");
		xml_parser_set_option($this->_parser,XML_OPTION_CASE_FOLDING,0);
		$this->_filename = $filename;
		
		if (file_exists($this->_filename)) {
			$fp = fopen($this->_filename,"r") or die("Error opening $this->_filename");
			$this->_data = fread($fp,filesize($this->_filename));
		}
	}
	
	function parse(){
		//die("$this->_data");
		if(!xml_parse_into_struct($this->_parser,$this->_data,$this->_struct,$this->_index)){
			return false;
		}
		xml_parser_free($this->_parser);
	}
	
	function get_elements($tagnames,$level=null,$type=null){
		$ret = array();
		if (!is_array($tagnames)) {
			$tagnames = array($tagnames);
		}
		foreach ($tagnames as $tagname){
			if (is_array($this->_index[$tagname])) {
				$k = count($ret[$tagname]);
				foreach ($this->_index[$tagname] as $v){
					if ($level===null || ($level !== null && $this->_struct[$v]["level"]==$level)) {
						if (!is_array($type) || (is_array($type) && in_array($this->_struct[$v]["type"],$type))) {
							$ret[$tagname][] = $this->_struct[$v];
						}
					}
				}
			}
		}
		return $ret;
	}
	
}

?>